package ms.maplestory.boot.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

public class JpaService<T, ID> {

    @Autowired
    private JpaRepository<T, ID> repository;

    public List<T> findAll(Sort var1) {
        return repository.findAll(var1);
    }

    public Page<T> findAll(Pageable var1) {
        return repository.findAll(var1);
    }

    public <S extends T> S save(S var1) {
        return repository.save(var1);
    }

    public <S extends T> S saveAndFlush(S var1) {
        return repository.saveAndFlush(var1);
    }

    public <S extends T> Iterable<S> saveAll(Iterable<S> var1) {
        return repository.saveAll(var1);
    }

    public T findById(ID var1) {
        Optional<T> obj = repository.findById(var1);
        return obj.orElse(null);

    }

    public boolean existsById(ID var1) {
        return repository.existsById(var1);
    }

    public List<T> findAll() {
        return repository.findAll();
    }

    public List<T> findAllById(Iterable<ID> var1) {
        return repository.findAllById(var1);
    }

    public long count() {
        return repository.count();
    }

    public void deleteById(ID var1) {
        repository.deleteById(var1);
    }

    public void delete(T var1) {
        repository.delete(var1);
    }

    public void deleteAll(Iterable<? extends T> var1) {
        repository.deleteAll(var1);
    }

    public void deleteAll() {
        repository.deleteAll();
    }

    @Transactional
    public void deleteByIds(List<ID> ids) throws RuntimeException {
        for (ID id : ids) {
            this.deleteById(id);
        }
    }

    public JpaRepository<T, ID> getRepository() {
        return repository;
    }

}